
package biblio.dao;


import biblio.domain.Reader;
import biblio.exception.NoSuchEntityException;
import java.io.Serializable;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


/**
 * Default implementation of ReaderDAO.
 * @author mephisto
 */
public class ReaderDAOImpl extends HibernateDaoSupport implements ReaderDAO
{

    public void create(Reader reader)
    {
        getHibernateTemplate().persist(reader);
    }

    public Reader getById(Integer id) throws NoSuchEntityException
    {
        Reader val = (Reader) getHibernateTemplate().get(Reader.class, id);
        if (val == null)
            throw new NoSuchEntityException("No Reader identified by id: " + id);
        return val;
    }

    /**
     * @throws DataAccessException - in case of Hibernate errors
     */
    public List<Reader> find(Serializable lastName) throws NoSuchEntityException
    {
        List<Reader> res = getHibernateTemplate().findByNamedParam(
                "from Reader where lastName = :lastName", "lastName", lastName);
        if (res.isEmpty())
            throw new NoSuchEntityException("No reader under lastName " + lastName);
        return res;
    }

    /**
     * @throws DataAccessException - in case of Hibernate errors
     */
    public void update(Reader reader) throws NoSuchEntityException
    {
        getHibernateTemplate().update(reader);
    }

    /**
     * @throws DataAccessException - in case of Hibernate errors
     */
    public void delete(Reader reader)
    {
        getHibernateTemplate().delete(reader);
    }

    /**
     * @throws DataAccessException - in case of Hibernate errors
     */
    public List<Reader> selectAll()
    {
        return getHibernateTemplate().find("from Reader");
    }
}
